package com.example.springboot.mapper;

import com.example.springboot.entity.User;
import com.example.springboot.entity.UserEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {
//    CREATE TABLE `sys_users` (
//            `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
//            `username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
//  `password` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `nickname` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `phone` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
//            `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
//  `avatar` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `all_views` int(255) unsigned DEFAULT NULL,
//  `article_num` int(255) unsigned DEFAULT NULL,
//  `fens_num` int(255) unsigned DEFAULT NULL,
//  `ip_address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `signature` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//  `birthday` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
//    PRIMARY KEY (`id`)
//) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    @Select("select password from sys_users where username = #{username}")
    String getPassword(UserEntity user);

    @Select("select id,username,password,nickname,email,phone,create_time as createTime,update_time as updateTime," +
            "avatar,all_views as allViews,article_num as articleNum,fens_num as fensNum,ip_address as ipAddress," +
            "signature,birthday from sys_users where username = #{username}")
    UserEntity findByUsername(String username);
    @Select("select id,username,nickname,email,phone,create_time as createTime,update_time as updateTime," +
            "avatar,all_views as allViews,article_num as articleNum,fens_num as fensNum,ip_address as ipAddress," +
            "signature,birthday from sys_users where username = #{username}")
    User findByUsername2(String username);
    @Select("select id,username,nickname,email,phone,create_time as createTime,update_time as updateTime," +
            "avatar,all_views as allViews,article_num as articleNum,fens_num as fensNum,ip_address as ipAddress," +
            "signature,birthday from sys_users where id = #{authorId}")
    User findById(Integer authorId);
    void updateUserInfo(User user);
    @Select("select id,username,nickname,email,phone,create_time as createTime,update_time as updateTime," +
            "avatar,all_views as allViews,article_num as articleNum,fens_num as fensNum,ip_address as ipAddress," +
            "signature,birthday from sys_users where username = #{username}")
    User getUserByUsername(String username);

    List<User> findAll();

    void increFansNum(Integer followedId);

    void decreFansNum(Integer followedId);
    @Select("select * from sys_users where open_id = #{openId} limit 1")
    User selectUserByOpenId(User user);
    @Insert("INSERT INTO sys_users (username, password, nickname, email, phone, create_time, update_time, avatar, all_views, article_num, fens_num, ip_address, signature, birthday, open_id) " +
            "VALUES (#{username}, #{password}, #{nickname}, #{email}, #{phone}, #{createTime}, #{updateTime}, #{avatar}, #{allViews}, #{articleNum}, #{fensNum}, #{ipAddress}, #{signature}, #{birthday}, #{openId})")
    void insert(User user);
    @Update("UPDATE sys_users SET password = #{newPassword} WHERE id = #{userId} AND username = #{username}")
    void changePassword(Integer userId, String username, String newPassword);
    @Insert("INSERT INTO sys_users (username, password, nickname, email, phone, create_time, update_time, avatar, all_views, article_num, fens_num, ip_address, signature, birthday, open_id) " +
            "VALUES (#{username}, #{password}, #{nickname}, #{email}, #{phone}, #{createTime}, #{updateTime}, #{avatar}, #{allViews}, #{articleNum}, #{fensNum}, #{ipAddress}, #{signature}, #{birthday}, #{openId})")
    void register(UserEntity user);
}
